class State:
    def handle(self, context):
        raise NotImplementedError("Subclasses must implement this method")


class ConcreteStateA(State):
    def handle(self, context):
        print("Handling state A")
        # 假设在状态A中，我们想要改变到状态B
        context.set_state(ConcreteStateB())


class ConcreteStateB(State):
    def handle(self, context):
        print("Handling state B")
        # 假设在状态B中，我们想要改变到状态A
        context.set_state(ConcreteStateA())


class Context:
    def __init__(self):
        self._state = None

    def set_state(self, state):
        self._state = state

    def request(self):
        if self._state is not None:
            self._state.handle(self)

        # 使用示例


if __name__ == "__main__":
    context = Context()
    context.set_state(ConcreteStateA())

    # 客户端代码只需要与上下文交互
    context.request()  # 输出: Handling state A
    context.request()  # 输出: Handling state B
    context.request()  # 输出: Handling state A